//
//  WinViewController.m
//  CarTown
//
//  Created by xionghao on 15/8/6.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "WinViewController.h"
#import "WinCellTableViewCell.h"
#import "WindetailViewController.h"
#import "DOPDropDownMenu.h"
#import "Brand.h"
#import "CarSourceType.h"
#import <MJRefresh.h>
#import "WinModel.h"

@interface WinViewController ()<UITableViewDataSource,UITableViewDelegate,DOPDropDownMenuDataSource,DOPDropDownMenuDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *brandArray;
@property (nonatomic,strong) NSMutableArray *cartoryArray;
@property (nonatomic,strong) NSMutableArray *locationArray;

@property (nonatomic,strong) NSArray *carsoucearray;
@property (nonatomic,strong) NSArray *brandindexarray;

@property(nonatomic,strong) NSMutableArray *menuarray;
@property (nonatomic,strong) NSMutableArray *winlist;
@property (nonatomic,strong) NSTimer *timer;
@end

@implementation WinViewController

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
-(NSMutableArray *)winlist{
    if ( _winlist==nil) {
        _winlist=[NSMutableArray array];
    }
    return  _winlist;
}
-(NSMutableArray *)locationArray{
    if ( _locationArray==nil) {
        _locationArray=[NSMutableArray array];
    }
    return  _locationArray;
}
-(NSMutableArray *)menuarray{
    if ( _menuarray==nil) {
        _menuarray=[NSMutableArray array];
    }
    return  _menuarray;
}
-(NSMutableArray *)cartoryArray{
    if ( _cartoryArray==nil) {
        _cartoryArray=[NSMutableArray array];
    }
    return  _cartoryArray;
}
-(NSMutableArray *)brandArray{
    if ( _brandArray==nil) {
        _brandArray=[NSMutableArray array];
    }
    return  _brandArray;
}
-(NSArray *)brandindexarray{
    if ( _brandindexarray==nil) {
        _brandindexarray=[Brand getBrand];
    }
    return  _brandindexarray;
}

-(NSArray *)carsoucearray{
    if (_carsoucearray==nil) {
        _carsoucearray=[CarSourceType getCarSourceType];
    }
    return _carsoucearray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setuptableview];
    
    self.tableview.rowHeight=121;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"WinCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"wincell"];
    [self setupdata];
    
    DOPDropDownMenu *menu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, 116) andHeight:40];
    menu.dataSource = self;
    menu.delegate = self;
    [self.view addSubview:menu];
}

-(void)setuptableview{
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    
    self.tableview.header=header;
    [self.tableview.header beginRefreshing];
}
-(void)refresh{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"where";
    params[@"num"]=Kpagesize;
    
    [PostUitls Post:lookforzhaobiaourl parameters:params success:^(id response) {
        [self removeobservers];
        [self.winlist removeAllObjects];
        NSDictionary *dict=response;
        
        
        [WinModel setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{
                     @"ID":@"id"
                     };
        }];
        
        
        for (NSDictionary *mdict in dict[@"data"]) {
           WinModel *mode=[WinModel objectWithKeyValues:mdict];
            [mode addObserver:self forKeyPath:@"last_time" options:NSKeyValueObservingOptionNew context:nil];
            [self.winlist addObject:mode];
        }
        
        
        [self.tableview reloadData];
        [self.tableview.header endRefreshing];
        [self setuptimer];
    } failure:^(NSError *error) {
        
    }];
}
-(void)removeobservers{
    for (WinModel *mode in self.winlist) {
        [mode removeObserver:self forKeyPath:@"last_time"];
    }
}
-(void)updatetime{
    
    for (WinModel *mode in self.winlist) {
        if ([mode.last_time integerValue]==0) {
            continue;
        
        }
        NSInteger last=[mode.last_time integerValue]-60;
        if (last<0) {
            last=0;
        }
        mode.last_time=[NSNumber numberWithInteger: last];
        
    }
    
}

-(void)setuptimer{
   self.timer=[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(updatetime) userInfo:nil repeats:YES];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"last_time"]&&[object isKindOfClass:[WinModel class]]) {
        WinModel *mode=object;
        NSInteger index=[self.winlist indexOfObject:mode];
        NSIndexPath *indexpath=[NSIndexPath indexPathForRow:index inSection:0];
        WinCellTableViewCell *cell=[self.tableview cellForRowAtIndexPath:indexpath];
        cell.mode=mode;
    }
}

-(void)setupdata{
    [self.brandArray addObject:@"品牌"];
    for (Brand *brand in self.brandindexarray) {
        [self.brandArray addObject:[NSString stringWithFormat:@"%@-%@",brand.brand_desc,brand.brand_name]];
    }
    [self.cartoryArray addObject:@"车源类型"];
    NSArray *array=[CarSourceType getCarSourceType];
    for (CarSourceType *carsource in array) {
        [self.cartoryArray addObject:carsource.name];
    }
    [self.locationArray addObjectsFromArray:@[@"交货地点",@"东区",@"西区",@"南区",@"北区",@"中区"]];
    [self.menuarray addObject:self.brandArray];
    [self.menuarray addObject:self.cartoryArray];
    [self.menuarray addObject:self.locationArray];
}

- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu {
    return self.menuarray.count;
}
-(void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row);
}
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column {
    NSArray *array=self.menuarray[column];
    return array.count;
}
- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath {
     NSArray *array=self.menuarray[indexPath.column];
    return array[indexPath.row];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.winlist.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WinCellTableViewCell *cell=[WinCellTableViewCell cellWithTableview:tableView];
    WinModel *mode=self.winlist[indexPath.row];
    cell.mode=mode;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WindetailViewController *windetail=[WindetailViewController viewcontroler];
    [self.navigationController pushViewController:windetail animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
